#!/bin/bash -e

function info() {
echo Usage: `basename $0` '[-lnsgGac] reads1.fq.gz reads2.fq.gz'
exit 65
}

while getopts  ":l:p:f:n:s:gGa:c" opts; do
	case  $opts  in
	a) cut=$OPTARG;;
	c) cnv=T;;
	l) interval=$OPTARG;;
	n) nums=$OPTARG;;
	s) sample_name=$OPTARG;;
	g) single_cell=T;;
	G) somatic=T;;
	p) out_prefix=$OPTARG;;
	f) suffix=$OPTARG;;
	*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 2 ]; then info; fi

r1=`rel_abs.sh $1`
r2=`rel_abs.sh $2` # must run before mkdir=true

makedir=true

if test -z "$interval"; then interval=$data_path/intervals/1/b37.bed; fi


. $var


split_fastq=split_fastq.sh

if test -z "$cut"; then
cutadp=cutadp.sh
elif test "$cut" = "general"; then
cutadp=cutadp.sh
elif test "$cut" = "malbac"; then
cutadp=cutadp_malbac.sh
elif test "$cut" = "ctc80"; then
cutadp=cutadp_ctc80_discard_trimmed.sh
fi

aln=aln_bwa_bt.sh
sam2b=sam2b_samtools.sh
merge=merge_bam.sh
hmmcopy=hmmcopyb38.sh
split_bam=split_bam_0701.sh
extract_bam_bed=extract_bam_bed.sh
markdup=deldup.sh
realn=realign_bt.sh
recal=recal0630.sh
ug=ug.sh
annos=annos0711.sh
combine_vcfs=combine_vcfs.sh
impact=impact.sh
format_simple=format_simple_0629.sh


pigz -cdf $r1|$split_fastq -l4000000 -p$out_prefix.1.split - &
pigz -cdf $r2|$split_fastq -l4000000 -p$out_prefix.2.split -

wait

cpu2=2
cpu4=4
cpu8=8
mem4=4
mem8=8
mem16=16
mem32=32


fq_list=$out_prefix.fq.list.txt
cutadp_list=$out_prefix.cutadp.list.txt
sam_list=$out_prefix.sam.list.txt
bam_list=$out_prefix.bam.list.txt
bed_list=$out_prefix.bed.list.txt
merge_list=$out_prefix.merge.list.txt
split_list=$out_prefix.split.list.txt
markdup_list=$out_prefix.markdup.list.txt

i_bed_list=$out_prefix.i.bed.list.txt
l_bed_list=$out_prefix.l.bed.list.txt
realn_list=$out_prefix.realn.list.txt
recal_list=$out_prefix.recal.list.txt
ug_list=$out_prefix.ug.list.txt
high_list=$out_prefix.high.list.txt
low_list=$out_prefix.low.list.txt
hl_list=$out_prefix.hl.list.txt
combine_list=$out_prefix.combine.list.txt
impact_high_list=$out_prefix.impact.high.list.txt
impact_low_list=$out_prefix.impact.low.list.txt
impact_hl_list=$out_prefix.impact.hl.list.txt
end_list=$out_prefix.end.list.txt


ls $out_prefix.1.split.* > $out_prefix.1.list.txt
ls $out_prefix.2.split.* > $out_prefix.2.list.txt
paste -d' ' $out_prefix.1.list.txt $out_prefix.2.list.txt >$fq_list && rm $out_prefix.1.list.txt $out_prefix.2.list.txt

IFS=$'\n'
i=0
for item in $(cat $fq_list); do
	i=$((i+1))
	IFS=$' \n'
	$cutadp -p$out_prefix.$i $item &
	IFS=$'\n'
	echo $out_prefix.$i.1.cut.fastq $out_prefix.$i.2.cut.fastq >> $cutadp_list
	echo $out_prefix.$i.sam >> $sam_list
	echo $out_prefix.$i.sort.bam >> $bam_list

done



monitor0721.sh "$aln -s$sample_name -t1" $cutadp_list $cutadp $fq_list &

monitor0721.sh "$sam2b -t1" $sam_list $aln $cutadp_list &

wait

$merge -t8 $bam_list $sam2b $sam_list

merge_in_file=$out_prefix.merge.in.txt
echo $bam_list >> $merge_in_file

if test -n "$cnv"; then
	monitor0721a.sh -ndel -m20 -c2 "$markdup" $out_prefix.merge.bam $merge $merge_in_file
	# can without using monitor, can using subq.sh directly

	merge_bam_list=$out_prefix.merge.bam.list.txt
	monitor0721a.sh -nhmm -m20 -c2 "$hmmcopy -b37" $out_prefix.deldup.bam $markdup $merge_bam_list
fi

if test -z "$nums"; then nums=120; fi


$split_bam $out_prefix.merge.bam $interval $nums &


for i in $(seq $nums); do
	echo $out_prefix.$i.bed >> $bed_list
	echo $out_prefix.$out_prefix.$i.bed.bam >> $split_list #note that there are 2 out_prefix

	echo $out_prefix.$i.markdup.bam >> $markdup_list
	echo $out_prefix.$i.realn.bam >> $realn_list
	echo $out_prefix.$i.realn.recal.bam >> $recal_list
	echo $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf >> $ug_list
	echo $out_prefix.$i.high.vcf >> $high_list
	echo $out_prefix.$i.low.vcf  >> $low_list
done

cat $bed_list|sed 's/^/'$out_prefix'.merge.bam /' > $merge_list

cat $bed_list|sed 's/^/-i/' > $i_bed_list
cat $bed_list|sed 's/^/-l /' > $l_bed_list

monitor0721.sh "$markdup -m" $split_list $extract_bam_bed $merge_list &

monitor0721.sh -i$i_bed_list "$realn -n4" $markdup_list $markdup $split_list &

monitor0721.sh -i$l_bed_list "$recal -t4" $realn_list $realn $markdup_list &

monitor0721.sh -i$i_bed_list $ug $recal_list $recal $realn_list &


if test -n "$somatic"; then
	echo; echo somatic
	if test -n "$single_cell"; then
		echo single_cell
		monitor0721.sh -nano -m12 -c6 -i$l_bed_list "$annos -gm" $ug_list $ug $recal_list &
	else
		monitor0721.sh -nano -m12 -c6 -i$l_bed_list "$annos -m" $ug_list $ug $recal_list &
	fi
else
	echo; echo germline
	if test -n "$single_cell"; then
		echo single_cell
		monitor0721.sh -nano -m12 -c6 -i$l_bed_list "$annos -g" $ug_list $ug $recal_list &
	else
		monitor0721.sh -nano -m12 -c6 -i$l_bed_list $annos $ug_list $ug $recal_list &
	fi
fi

wait

$combine_vcfs -p$out_prefix.high $high_list && $impact $out_prefix.high.combine.vcf && $format_simple -p$out_prefix.high $out_prefix.high.combine.af.hg19_multianno.vcf &

$combine_vcfs -p$out_prefix.low $low_list

$impact $out_prefix.low.combine.vcf

$format_simple -p$out_prefix.low $out_prefix.low.combine.af.hg19_multianno.vcf

wait

. $cmd_done